package com.liuzu.esys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuzu.esys.bo.OrderManageBo;
import com.liuzu.esys.common.LayuiPageVo;
import com.liuzu.esys.mapper.OrderManageMapper;
import com.liuzu.esys.pojo.OrderManage;
import com.liuzu.esys.service.OrderManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("/orderManageService")
public class OrderManageServiceImpl implements OrderManageService {
    @Autowired
    private OrderManageMapper orderManageMapper;

    @Override
    public LayuiPageVo<OrderManage> orderManagePageData(Map<String, Object> params) {
        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");
        PageHelper.startPage(page,limit);
        List<OrderManage> orderManages = orderManageMapper.selectByParams(params);

        PageInfo<OrderManage> orderManagePageInfo = new PageInfo<>(orderManages);
        //把mybatis 的 分页数据 封装到  layui 的分页数据结构中

        LayuiPageVo<OrderManage> orderManageLayuiPageVo = new LayuiPageVo<>();
        orderManageLayuiPageVo.setCode(0);  // layui 默认 0 是正确的状态码
        orderManageLayuiPageVo.setMsg("分页列表数据"); // 提示信息
        orderManageLayuiPageVo.setCount(orderManagePageInfo.getTotal()); // 总记录数
        orderManageLayuiPageVo.setData(orderManagePageInfo.getList());  // 分页的列表数据
        return orderManageLayuiPageVo;
    }

    @Override
    public int orderManageAdd(OrderManage orderManage) {
        return orderManageMapper.orderManageAdd(orderManage);
    }

    @Override
    public int orderManageUpdate(OrderManage orderManage) {
        return orderManageMapper.orderManageUpdate(orderManage);
    }

    @Override
    public int orderManageDelete(int order_number) {
        return orderManageMapper.orderManageDelete(order_number);
    }

    @Override
    public List<OrderManageBo> orderManageExcelData(OrderManage orderManage) {
        List<OrderManage> orderManages = orderManageMapper.selectBykey(orderManage);
        List<OrderManageBo> orderManageBos = orderManages.stream()
                .map((dr) -> {
                    OrderManageBo orderManageBo = new OrderManageBo();
                    orderManageBo.setOrder_amount(dr.getOrder_amount());
                    orderManageBo.setOrder_status(dr.getOrder_status());
                    orderManageBo.setCreate_time(dr.getCreate_time());
                    orderManageBo.setPay_status(dr.getPay_status());
                    orderManageBo.setPhone(dr.getPhone());
                    orderManageBo.setPay_time(dr.getPay_time());
                    orderManageBo.setOrder_number(dr.getOrder_number());
                    return orderManageBo;
                }).collect(Collectors.toList());
        return orderManageBos;
    }


}
